<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 角色管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="addRole"
                >添加角色</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
<!--                <el-table-column type="selection" width="55" align="center"></el-table-column>-->
                <el-table-column prop="id" label="ID" width="130" align="center"></el-table-column>
                <el-table-column prop="roleName" label="名称"></el-table-column>
                <el-table-column prop="description" label="描述"></el-table-column>
                <el-table-column prop="gmtCreate" label="创建时间"></el-table-column>
                <el-table-column prop="status" label="状态">
                    <template #default="scope">
                        <el-switch active-color="#ff4949"
                                   v-model="scope.row.status"
                                   :active-value="1"
                                   :inactive-value="0" @change="prohibit(scope.row.id, scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >权限</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="详情" v-model="roleEditVisible" width="30%">
            <el-form ref="form" :model="role" label-width="70px">
                <el-form-item label="角色名">
                    <el-input v-model="role.roleName"></el-input>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input v-model="role.description"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="saveEdit">保 存</el-button>
                </span>
            </template>
        </el-dialog>

        <!-- 编辑弹出框 -->
        <el-dialog title="详情" v-model="editVisible" width="30%">
            <h3>已拥有权限</h3>
            <el-table
                    :data="existRight"
                    border
                    class="table2"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
            >
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="rightName" label="权限名"></el-table-column>
                <el-table-column prop="url" label="URL"></el-table-column>
                <el-table-column prop="description" label="描述"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button
                                v-if="scope.row.status==0"
                                type="text"
                                icon="el-icon-edit"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <h3>未拥有权限</h3>
            <el-table
                    :data="notExistRight"
                    border
                    class="table2"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
            >
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="rightName" label="权限名"></el-table-column>
                <el-table-column prop="url" label="URL"></el-table-column>
                <el-table-column prop="description" label="描述"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button
                                v-if="scope.row.status==0"
                                type="text"
                                icon="el-icon-edit"
                                @click="handleAdd(scope.$index, scope.row)"
                        >添加</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>

<script>
    import api from "../../../api";
    export default {
        name: "role",
        data() {
            return {
                query: {
                    role:{
                        id: "",
                        userId: "",
                        userName: "",
                        method: "",
                        ip: "",
                        url: "",

                    },
                    pageIndex: 1,
                    pageSize: 8
                },
                role:{
                    roleName: null,
                    description:null
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                roleEditVisible:false,
                pageTotal: 0,
                existRight: [],
                notExistRight:[],
                idx: -1,
                roleId: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            changeKey(val){
                this.query.key=val;
            },
            prohibit(id, row) {
                const status = row.status;
                api.prohibit(id, status,1).then(response => {
                    let msg = ""
                    if (status=='0') {
                        msg = "激活成功"
                    } else {
                        msg = "封禁成功"
                    }
                    this.$message({
                        type: 'success',
                        message: msg,
                    })
                    return response;
                }).catch(response => {
                    console.log(response.message)
                    this.$message({
                        type: 'error',
                        message: "操作失败",
                    })
                })
            },
            addRole() {
                this.roleEditVisible = true;
            },
            getData() {
                api.getRole(this.query.pageIndex,this.query.pageSize,this.query.log,).then(res => {
                    console.log(res);
                    this.tableData = res.data.item.records;
                    this.pageTotal = res.data.item.total;
                });
            },
            // 添加操作
            handleAdd(index,row) {
                let array = [];
                array[0]={"rightId":row.id};
                api.addRightToRole(array,this.roleId).then(resp=>{
                    if(resp.success){
                        this.$message.success("添加成功");
                        this.editVisible = false;
                    }else {
                        this.$message.error("添加失败");
                    }

                });
            },
            // 删除操作
            handleDelete(index,row) {
                // 二次确认删除
                this.$confirm("确定要删除吗？", "提示", {
                    type: "warning"
                }).then(() => {
                        let array = [];
                        array[0]={"rightId":row.id};
                        api.deleteRightToRole(this.roleId,array).then(resp=>{
                            if(resp.success){
                                this.$message.success("删除成功");
                                this.editVisible = false;
                            }else {
                                this.$message.error("删除失败");
                            }
                        });
                    })
                    .catch(() => {});
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            delAllSelection() {
                const length = this.multipleSelection.length;
                let str = "";
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + " ";
                }
                this.$message.error(`删除了${str}`);
                this.multipleSelection = [];
            },
            // 编辑操作
            handleEdit(index, row) {
                api.findRightByRoleId(row.id).then(resp=>{
                    console.log(resp);
                    this.existRight = resp.data.item.existRights;
                    this.notExistRight = resp.data.item.notExistRights;
                });
                this.roleId=row.id;
                this.idx = index;
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                api.addRole(this.role).then(resp=>{
                    if(resp.success) {
                        this.$message.success(`添加成功`);
                    }else {
                        this.$message.error(`添加失败`);
                    }
                });
                this.roleEditVisible = false;
            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, "pageIndex", val);
                this.getData();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
